// 封装倒计时
import { computed, onUnmounted, ref } from "vue"
import dayjs from "dayjs"

export const useCountDown = () =>{
    // 1.响应式数据
    const time = ref(0)

    let timer = null
    // 格式化事件
    const formatTime = computed(()=>{
        return dayjs.unix(time.value).format('mm分ss秒')
    })
    // 2.开启倒计时函数
    const start = (currenTime) =>{
        time.value = currenTime
        timer = setInterval(()=>{
            if(time.value <= 0){
                time.value = 0
                clearInterval(timer)
            }else{
                time.value--
            }
        },1000)
    }

    onUnmounted(()=>{
        clearInterval(timer)
    })

    return{
        formatTime,
        start
    }
}